package com.mk.expression;

import java.math.BigDecimal;

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import com.mk.settlement.common.FeeUtil;
import com.mk.settlement.model.Order;

public class SpelRegister {

    public static void main(String[] args) {

        Order o = new Order();
        
        o.setOtaid(111l);
        o.setOprice(new BigDecimal(20));
        
        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext(o);

        try {
            context.registerFunction("calFee",
                FeeUtil.class.getDeclaredMethod("calFee", new Class[] { Long.class,BigDecimal.class }));
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        BigDecimal result = parser.parseExpression(
            "#calFee(otaid,oprice)").getValue(context, BigDecimal.class);

        System.out.println(result.floatValue());
    }

}
